package com.cysyz.mylib.datacopytool;

import com.cysyz.mylib.datacopytool.getterandsetteradapter.GetterAndSetterAdapter;
import com.cysyz.mylib.datacopytool.getterandsetteradapter.JavaBeanGetterAndSetterAdapter;
import com.cysyz.mylib.utils.myassert.MyAssert;

import java.util.regex.Pattern;

public class SimpleDataCopyTool implements DataCopyTool {
    private GetterAndSetterAdapter from;
    private GetterAndSetterAdapter to;

    public SimpleDataCopyTool(GetterAndSetterAdapter from, GetterAndSetterAdapter to) {
        this.from = from;
        this.to = to;
    }

    public SimpleDataCopyTool() {
        this.from = new JavaBeanGetterAndSetterAdapter();
        this.to = new JavaBeanGetterAndSetterAdapter();
    }

    @Override
    public void copyValues(Object original, Object target, String... valueIdPairs) {
        MyAssert.notNullMulti(original, target, valueIdPairs);
        if (valueIdPairs != null && valueIdPairs.length == 0) {
            if (from.getGetterAndSetterType() != to.getGetterAndSetterType()) {
                throw new IllegalArgumentException("当不填写valueIdPairs时，意味着要把original中的所有数据拷贝至target中，此时的from和to的GetterAndSetterType必须一致");
            }
            valueIdPairs = from.getAllValueIds(original);

        }

        for (int i = 0; i < valueIdPairs.length; i++) {
            String[] origAndTargetFieldNames = valueIdPairs[i].split(":");
            if (origAndTargetFieldNames.length == 2 || origAndTargetFieldNames.length == 1) {
                String originalFieldName = null;
                String targetFieldName = null;
                if (origAndTargetFieldNames.length == 2) {
                    originalFieldName = origAndTargetFieldNames[0];
                    targetFieldName = origAndTargetFieldNames[1];
                } else if (origAndTargetFieldNames.length == 1) {
                    GetterAndSetterType inputValueIdType = null;
                    Pattern pattern = Pattern.compile("[\\d\\$\\w_]+");
                    if (pattern.matcher(origAndTargetFieldNames[0]).matches()) {
                        inputValueIdType = GetterAndSetterType.KEYWORD;
                    } else {
                        throw new IllegalArgumentException("当valueIdPairs不是：分割形式的字符串时，它只能是一个符合java变量命名规则的字符串");
                    }
                    if (from.getGetterAndSetterType() == inputValueIdType) {
                        originalFieldName = origAndTargetFieldNames[0];
                    }
                    if (to.getGetterAndSetterType() == inputValueIdType) {
                        targetFieldName = origAndTargetFieldNames[0];
                    }

                    if (originalFieldName == null) {
                        originalFieldName = i + "";
                    }
                    if (targetFieldName == null) {
                        targetFieldName = i + "";
                    }

                }
                if (from.supports(original) && to.supports(target)) {
                    to.setValue(target, targetFieldName, from.getValue(original, originalFieldName));
                }
            } else {
                throw new IllegalArgumentException("valueIdPairs格式错误，必须是【数据来源对象的属性名称】:【目标对象的属性名称】格式：" + origAndTargetFieldNames);
            }
        }
    }
}

